<template>
  <div>
    <el-upload
      v-show="!isshowProgress"
      class="avatar-uploader"
      action="#"
      :http-request="onRequest"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="onPreview"
      :on-remove="onRemove"
      accept="image/jpg"
      :before-upload="beforeUpload"
      :on-change="onChange"
      :limit="limit"
      :on-exceed="onExtend"
    >
      <i class="el-icon-plus avatar-uploader-icon" />
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>

    <el-progress v-if="isshowProgress" type="circle" :percentage="percentage" />

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <img :src="imgurl">
    </el-dialog>

  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
// console.log(COS)
// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKID0pNgiTxB8oCJ9i02oLeiX4KJ0RaS3OSF',
  SecretKey: 'XpqZ1FEyRdYCZfV4MNjoc9PO6XhaBWaN'
})
// console.log(cos)
export default {
  name: 'Uploadphotosss',
  props: {
    limit: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      fileList: [],
      dialogVisible: false,
      imgurl: '',
      percentage: 0,
      isshowProgress: false
    }
  },

  created() {

  },

  methods: {
    onRequest(data) {
      this.isshowProgress = true
      this.id = setInterval(() => {
        if (this.percentage < 99) {
          this.percentage++
        } else {
          clearInterval(this.id)
          this.percentage = 99
        }
      }, 800)
      console.log(data)
      cos.putObject({
        Bucket: 'hrsass-1308960785', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: data.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          // console.log(JSON.stringify(progressData))
          if (progressData.percent === 1) {
            this.percentage = 100
            clearInterval(this.id)
            setTimeout(() => {
              this.isshowProgress = false
            }, 500)
          }
        }
      }, (err, data) => {
        if (err) {
          this.isshowProgress = false
          this.$message.error('网络错误,上传失败')
          this.fileList.splice(this.fileList.length - 1, 1)
        }
        console.log(err || data)
        this.fileList[this.fileList.length - 1].url = 'https://' + data.Location
        this.$emit('on-success', this.fileList)
      })
    },
    onPreview(file) {
      this.imgurl = file.url
      this.dialogVisible = true
    },
    onRemove(file, filelist) {
      this.fileList = filelist
      console.log(this.fileList)
    },
    beforeUpload(file) {
      console.log(file)

      // this.percentage = this.file
      console.log(file)
      const imgType = ['image/jpeg']
      if (!imgType.includes(file.type)) {
        this.$message.error('请上传正确的格式图片')
        return false
      }
      const maxSize = 1 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('请上传1M以内的')
        return false
      }
    },
    onChange(file, fileList) {
      this.fileList = fileList
    },
    onExtend() {
      this.$message.error('只能上传' + this.limit + '张图片')
    }
  }
}
</script>

<style  lang='scss'>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    height: 148px;
    width: 148px;
    height: 148px;
    text-align: center;
  }

</style>
